package com.zhuoshi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhuoshi.bean.WdHtRoom;
import com.zhuoshi.bean.WdHtRoomPay;
import com.zhuoshi.bean.WdUser;
import com.zhuoshi.service.WdRoomPayService;
import com.zhuoshi.service.WdRoomService;

@Controller
@RequestMapping("/roompay")
public class WdRoomPayController {

	@Autowired
	private WdRoomPayService roomPayService;
	@Autowired
	private WdRoomService roomService;
	/**
	 * 查询所有
	 */ 
	@RequestMapping("/list.do")
	 public String list(WdHtRoomPay wbean ,Model m){
		if(wbean.getCustomerName()!=null){
		System.out.println(wbean.getCustomerName());
		}
		if(wbean.getCustomerName() != null && "".equals(wbean.getCustomerName().trim())){
			wbean.setCustomerName(null);
		}
		if(wbean.getName() != null && "".equals(wbean.getName().trim())){
			wbean.setName(null);
		}
		if(wbean.getUserId() != null && "".equals(wbean.getUserId().trim())){
			wbean.setUserId(null);
		}
		m.addAttribute("plist", roomPayService.selectAll(wbean));
		return "roompay_list";
		 
	 }
	@RequestMapping("/zuofei.do")
	public String zuofei(String roomPayId){
		WdHtRoomPay payBean = new WdHtRoomPay();
		payBean.setRoomPayId(roomPayId);
		payBean.setDeleteState("2");
		int updateByPrimaryKeySelective = roomPayService.updateByPrimaryKeySelective(payBean);
		return "redirect:list.do";
	}
	@RequestMapping("/delete.do")
	public String delete(String roomPayId,HttpServletRequest req){
		WdHtRoomPay payBean = new WdHtRoomPay();
		payBean.setRoomPayId(roomPayId);
		payBean.setDeleteState("1");
		payBean.setDeleteTime(new Date());
		roomPayService.updateByPrimaryKeySelective(payBean);
		return "redirect:list.do";
	}
	@RequestMapping("/selectAllRoom.do")
	public Map<String, Object> selectRoom(HttpServletResponse resp) {
		WdHtRoomPay wbean = new WdHtRoomPay();
		List<WdHtRoomPay> selectAll = roomPayService.selectAll(wbean);
		System.out.println(selectAll.isEmpty());
		List<WdHtRoom> rlist = new ArrayList<WdHtRoom>();
		for(WdHtRoomPay bean : selectAll){
			if("0".equals(bean.getDeleteState())){
			rlist.add( roomService.selectByPrimaryKey(bean.getRoomId()));
			}
			}
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		List list = null;
		if (rlist != null && rlist.size() > 0) {
			list = new ArrayList();
			for (WdHtRoom bean : rlist) {
				System.out.println("--------------------"+bean.getRoomId());
				System.out.println("--------------------"+bean.getRoomCode());
				Map<String, String> map = new HashMap<String, String>();
				map.put("roomId", bean.getRoomId());
				map.put("roomName", bean.getRoomCode());
				list.add(map);
			}
		}
		jsonMap.put("tasks", list);
		try {
			resp.getWriter().print(jsonMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonMap;
	}
	@RequestMapping("/shoukuan.do")
	public String shoukuan(String roomPayId,Model m){
		m.addAttribute("roomPayId", roomPayId);
		return "roompay_shouqian";
	}
	@RequestMapping("/updateMoney")
	public String updateMoney(String roomPayId ,int Money){
		WdHtRoomPay payBean = new WdHtRoomPay();
		payBean.setRoomPayId(roomPayId);
		WdHtRoomPay pbean = roomPayService.selectByPrimaryKey(roomPayId);
		payBean.setUncollectedMoney(pbean.getUncollectedMoney()-Money);
		payBean.setReceivedMoney(pbean.getReceivedMoney()+Money);
		payBean.setPayTime(new Date());
		payBean.setFinishTimes(pbean.getFinishTimes()+1);
		int updateByPrimaryKeySelective = roomPayService.updateByPrimaryKeySelective(payBean);
		return "redirect:list.do";
	}
	@RequestMapping("/returnMoney")
	public String returnMoney(String roomPayId ){
		WdHtRoomPay payBean = new WdHtRoomPay();
		for(WdHtRoomPay bean :roomPayService.selectAll(payBean)){
			if(bean.getRoomPayId().equals(roomPayId)){
				payBean.setRoomPayId(roomPayId);
				payBean.setReceivedMoney(0f);
				payBean.setUncollectedMoney(bean.getReceivableMoney());
				payBean.setDeleteState("3");
				roomPayService.updateByPrimaryKeySelective(payBean);
				
			}
		}
		return "redirect:list.do";
	}
	@RequestMapping("/roomMoney")
	public String roomMoney(String roomId ){
		WdHtRoomPay wbean = new WdHtRoomPay();
		for( WdHtRoomPay bean :roomPayService.selectAll(wbean)){
			if(bean.getRoomId().equals(roomId)){
				WdHtRoomPay payBean = new WdHtRoomPay();
				payBean.setRoomPayId(bean.getRoomPayId());
				payBean.setReceivedMoney(0f);
				payBean.setUncollectedMoney(bean.getReceivableMoney());
				payBean.setDeleteState("3");
				roomPayService.updateByPrimaryKeySelective(payBean);
					
			}
		}
		return "redirect:list.do";
	}
}
